﻿using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections.Generic;
using System.Data;
using System.Speech.Synthesis;
using System.Windows.Forms;
using Titan.FIS.Barcode.TwoBarcode;
using Titan.FIS.Enum;
using Titan.FIS.UI;

namespace Titan.FIS.SampleRecd
{
    public partial class frmReceiveSample : Form
    {
        private readonly IList<UltraGridRow> _listSelectRow = new List<UltraGridRow>();//选中的设备，用于打印
        private readonly int _iPrintBarcodeType = 2;
        /// <summary>
        /// 构造函数
        /// </summary>
        public frmReceiveSample(int iPrintBarcodeType)
        {
            InitializeComponent();
            _iPrintBarcodeType = iPrintBarcodeType;
        }

        /// <summary>
        /// 窗体初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCollect_Load(object sender, EventArgs e)
        {
            this.ActiveControl = txtBarCode;
            DoQuery();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCollect_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
        private void DoQuery()
        {
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQueryCollectSampleInfo(ref tabSource);
            if (string.IsNullOrEmpty(errMsg))
            {
                Titan.FIS.Data.MergeDataTable.MergeCaption(tabMain, ref tabSource);
                GridMain.DataSource = tabSource;
            }
            else
            {
                GridMain.DataSource = tabMain;
            }
        }
        /// <summary>
        /// 分样号输入框点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_Click(object sender, EventArgs e)
        {
            txtBarCode.Text = "";
        }

        /// <summary>
        /// 延迟系统时间，但系统又能同时能执行其它任务；
        /// </summary>
        /// <param name="Millisecond"></param>
        public static void Delay(int Millisecond) //延迟系统时间，但系统又能同时能执行其它任务；
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(Millisecond) > DateTime.Now)
            {
                Application.DoEvents();//转让控制权            
            }
            return;
        }

        private void btnSubSample_Click(object sender, EventArgs e)
        {
            txtBarCode.Text = "";
            txtSubCode.Text = "";

            this.ActiveControl = btnPrint;
        }


        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (GridMain.ActiveRow == null)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先点击想要复制的行!");
                return;
            }
            var collectCode = GridMain.ActiveRow.Cells["COLLECT_CODE"].Value.ToString();
            if (!string.IsNullOrEmpty(collectCode))
            {
                Clipboard.SetText(collectCode);
                FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("复制分样号【{0}】成功！", collectCode));
            }
            else
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("分样号为空，请检查！"));
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DoPrintCode();
            this.ActiveControl = txtBarCode;
        }

        private void btnPrintRfid_Click(object sender, EventArgs e)
        {
            DoPrintRfid();
            this.ActiveControl = txtPrintRfid;
        }

        /// <summary>
        /// 打印收样编码
        /// </summary>
        private void DoPrintCode()
        {
            var collectCode = txtSubCode.Text.Trim();
            if (string.IsNullOrEmpty(collectCode))
            {
                if (GridMain.ActiveRow == null)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先分样或者选择分样号!");
                    return;
                }
                collectCode = GridMain.ActiveRow.Cells["COLLECT_CODE"].Value.ToString();
            }
            if (MessageBox.Show(string.Format("是否立即打印分样号【{0}】？", collectCode), "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            var gen = new TagGen();
            var img = gen.GetImgASample(collectCode + "号样");
            var obj = new TagPrint(img, 1);
            if (!obj.Print())
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("打印分样号{0}失败!", collectCode));
            }
            FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("打印分样号{0}成功!", collectCode));
        }

        /// <summary>
        /// 打印Rfid编码
        /// </summary>
        private void DoPrintRfid()
        {
            var collectCode = txtPrintRfid.Text.Trim();
            if (string.IsNullOrEmpty(collectCode))
            {
                if (GridMain.ActiveRow == null)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先扫卡或者选择要打印的行!");
                    return;
                }
                collectCode = GridMain.ActiveRow.Cells["RFID"].Value.ToString();
            }
            if (MessageBox.Show(string.Format("是否立即打印Rfid号【{0}】？", collectCode), "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            var strDesc = "样桶";
            var strDescSize = "60";
            var img = Barcode.TwoDimensionBarcode.Encode(QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeEncoder.ERROR_CORRECTION.M, "4", "7", collectCode, "", strDesc, strDescSize);
            img = Barcode.TwoDimensionBarcode.CombinImageFloor(img, "样桶RFID码", 12, 60);
            var obj = new TagPrint(img, _iPrintBarcodeType);
            if (!obj.Print())
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("打印Rfid号{0}失败!", collectCode));
            }
            txtPrintRfid.Text = "";
            ; FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("打印Rfid号{0}成功!", collectCode));
        }


        private void txtBarCode_TextChanged(object sender, EventArgs e)
        {
            if (txtBarCode.Text.Length == 16)
            {
                var strErrInfo = string.Empty;
                var strSampleNo = string.Empty;
                var strPcode = string.Empty;
                SpeechSynthesizer speech = new SpeechSynthesizer();
                speech.Volume = 100;
                var bSaveResult = DataOperate.DataExecProc.DoSubSample(txtBarCode.Text.Trim(), ref strPcode, ref strSampleNo, ref strErrInfo);
                if (!bSaveResult)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("样桶【{0}】分样失败,原因:{1}", txtBarCode.Text.Trim(), strErrInfo));
                    speech.SpeakAsync(string.Format("分样失败,{0}", strErrInfo));
                    txtBarCode.Text = "";
                }
                else
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("扫卡分样成功!分样号为【{0}】", strSampleNo));
                    speech.SpeakAsync(string.Format("分样号{0}", strSampleNo));
                    txtBarCode.Text = "";
                    txtSubCode.Text = strSampleNo;
                }
                DoQuery();
                this.ActiveControl = txtBarCode;
            }
        }


    }
}
